const log4js = require("log4js")
log4js.configure({
  appenders: { cheese: { type: "file", filename: "cheese.log" } },
  categories: { default: { appenders: ["cheese"], level: "error" } }
})
var logger = log4js.getLogger()
logger.level = "debug"

class FileListPlugin {
  constructor(options) {
    this.options = options
  }

  apply(compiler) {
    compiler.hooks.emit.tapAsync(
      'FileListPlugin',
      (compilation, callback) => {
        // logger.debug(compilation)
        var filelist = 'In this build:\n\n';

        // Loop through all compiled assets,
        // adding a new line item for each filename.
        for (var filename in compilation.assets) {
          filelist += '- ' + filename + '\n';
        }

        compilation.assets['filelist.md'] = {
          source: () => {
            return filelist
          },
          size: () => {
            return filelist.length
          }
        }

        callback()
      }
    )
  }
}

module.exports = FileListPlugin